<!DOCTYPE HTML>

<html>
<head>
<title>OffscreenCanvas transferToImageBitmap on worker thread: green and blue squares on white background.</title>
<style type="text/css">
.nomargin {
  margin: 0px auto;
}
</style>
<script id="myWorker" type="text/worker">
self.onmessage = function(e) {
  var canvas1 = new OffscreenCanvas(100, 100);
  var gl1 = canvas1.getContext('webgl');
  gl1.clearColor(0.0, 1.0, 0.0, 1.0);
  gl1.clear(gl1.COLOR_BUFFER_BIT);

  var canvas2 = new OffscreenCanvas(100, 100);
  var gl2 = canvas2.getContext('webgl');
  gl2.clearColor(0.0, 0.0, 1.0, 1.0);
  gl2.clear(gl2.COLOR_BUFFER_BIT);

  var image1 = canvas1.transferToImageBitmap();
  var image2 = canvas2.transferToImageBitmap();
  self.postMessage({bitmap1: image1, bitmap2: image2}, [image1, image2]);
};
</script>
<script>
var g_swapsBeforeAck = 15;

function makeWorker(script) {
   var blob = new Blob([script]);
   return new Worker(URL.createObjectURL(blob));
}

function main()
{
  draw();
}

function draw()
{
  var worker = makeWorker(document.getElementById("myWorker").textContent);

  worker.onmessage = function(msg) {
    var bitmap1 = msg.data.bitmap1;
    var bitmap2 = msg.data.bitmap2;
    var canvas1 = document.getElementById("canvas1");
    var ctx1 = canvas1.getContext('bitmaprenderer');
    ctx1.transferFromImageBitmap(bitmap1);

    var canvas2 = document.getElementById('canvas2');
    var ctx2 = canvas2.getContext('bitmaprenderer');
    ctx2.transferFromImageBitmap(bitmap2);
    waitForFinish();
  }
  
  worker.postMessage("");
}

function waitForFinish()
{
  if (g_swapsBeforeAck == 0) {
    domAutomationController.send("SUCCESS");
  } else {
    g_swapsBeforeAck--;
    document.getElementById('container').style.zIndex = g_swapsBeforeAck + 1;
    window.requestAnimationFrame(waitForFinish);
  }
}
</script>
</head>
<body onload="main()">
<div style="position:relative; width:200px; height:200px; background-color:white">
</div>
<div id="container" style="position:absolute; top:0px; left:0px">
<canvas id="canvas1" width="100" height="100" class="nomargin"></canvas>
</div>
<div id="container" style="position:absolute; top:0px; left:150px">
<canvas id="canvas2" width="100" height="100" class="nomargin"></canvas>
</div>
</body>
</html>
